package test;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.ym.EncryptUtil;

public class AddHeaderInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
	public AddHeaderInterceptor() {
		super(Phase.PREPARE_SEND);
	}
	@Override
	public void handleMessage(SoapMessage message) throws Fault {
		try{
			List<Header> headers=message.getHeaders();
			//创建doc对象
			Document doc=DOMUtils.createDocument();
			Element ele=doc.createElement("FCHeader");
			
			//此处元素应该按照服务器那边的要求
			Element element=doc.createElement("秘钥");
			element.setTextContent(EncryptUtil.encrypt("珍诚网络科技"));
			
			ele.appendChild(element);
			
			//把ele元素包装成Header，并添加到Soap的Headers列表中
			headers.add(new Header(new QName("FCQName"),ele));
		}catch (Exception e) {
			throw new Fault(new IllegalArgumentException(e.getMessage()));
		}
		
	}
	
}
